package com.lwb.ztx.main.model;

import com.lwb.ztx.bean.ProfileBean;
import com.lwb.ztx.utils.LogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LoadingJsonUtils {

    public static List<ProfileBean> readJsonLoadingBeans(String res) {
        List<ProfileBean> beans = new ArrayList<ProfileBean>();
        try {
            JSONObject profile = new JSONObject(res);
            JSONArray jsonArray = profile.optJSONArray("defaultChannel");
            if(jsonArray.length() > 0) {
                for (int i=0;i<jsonArray.length();i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    beans.add(getProfileBean(jsonObject));
                }
            }

            JSONArray more = profile.optJSONArray("more");
            if(more.length() > 0) {
                for (int i=0;i<more.length();i++) {
                    JSONObject jsonObject = more.getJSONObject(i);
                    beans.add(getProfileBean(jsonObject));
                }
            }
        } catch (Exception e) {
            LogUtils.e("readJsonNewsBeans error" , e);
        }
        return beans;
    }

    public static ProfileBean getProfileBean(JSONObject jsonObject) {
        ProfileBean bean = new ProfileBean();
        bean.setId(jsonObject.optString("id",""));
        bean.setName(jsonObject.optString("name",""));
        bean.setType(jsonObject.optString("type",""));
        bean.setApi(jsonObject.optString("api",""));
        bean.setOffline(jsonObject.optString("offline",""));
        return bean;
    }

}
